package com.ohos.carbon.slice.libraries;


import com.ohos.carbon.ResourceTable;
import com.ohos.carbon.slice.MainAbilitySlice;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.Image;
import ohos.agp.components.Text;
import ohos.agp.components.element.ElementScatter;

import java.util.Map;

import static com.ohos.carbon.slice.MainAbilitySlice.pf;

public class PicassoSlice extends AbilitySlice {

    public Image back_button, image, image1, image2;
    public Text text_title;

    private int requestCode = 123;

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_picasso);
        initView();
        image = (Image) findComponentById(ResourceTable.Id_image_view);
        Button button = (Button) findComponentById(ResourceTable.Id_button);
        button.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                Intent intent_picture_select = new Intent();
                presentForResult(new PictureSelectAbilitySlice(), intent_picture_select, requestCode);
            }
        });

        MainAbilitySlice.all = (Map<String, Integer>) pf.getAll();
        if (MainAbilitySlice.all.size() != 0) {
            if (MainAbilitySlice.all.containsKey("Picasso")) {
                for (Map.Entry<String, Integer> entry : MainAbilitySlice.all.entrySet()) {
                    if (entry.getKey().equals("Picasso")) {
                        int integer = entry.getValue();
                        integer++;
                        pf.putInt("Picasso", integer);
                        pf.flushSync();
                    }
                }
            } else {
                pf.putInt("Picasso", 1);
                pf.flushSync();
            }
        } else {
            pf.putInt("Picasso", 1);
            pf.flushSync();
        }
    }

    @Override
    protected void onResult(int requestCode, Intent resultIntent) {
        super.onResult(requestCode, resultIntent);
        if (this.requestCode == requestCode) {
            if (resultIntent != null) {
                int res_id = resultIntent.getIntParam("picture_res_id", ResourceTable.Media_icon);
                image.setImageAndDecodeBounds(res_id);
            }
        }
    }

    private void initView() {
        back_button = (Image) findComponentById(ResourceTable.Id_back_button);
        image1 = (Image) findComponentById(ResourceTable.Id_image1);
        image2 = (Image) findComponentById(ResourceTable.Id_image2);
        image1.setVisibility(Component.VISIBLE);
        image2.setVisibility(Component.VISIBLE);
        text_title = (Text) findComponentById(ResourceTable.Id_title_text);
        text_title.setText("Picasso");
        back_button.setImageElement(ElementScatter.getInstance(getContext()).parse(ResourceTable.Graphic_carbon_uparrow));
        back_button.setClickedListener(component -> onBackPressed());
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

}
